package me.xuym.crawler.simpleextension.filter;

import com.cbs.java.component.application.Application;
import me.xuym.crawler.Processor;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by broche on 16/08/2017.
 */
public class SimpleBlackListFilter extends Processor<List<SimpleBlackFilterInput>, List<SimpleBlackFilterInput>> {
    private List<String> blackKeys = new ArrayList<>();

    public SimpleBlackListFilter(Application application, List<String> blackKeys) {
        super(application);

        this.blackKeys.addAll(blackKeys);
    }

    @Override
    public List<SimpleBlackFilterInput> onProcess(List<SimpleBlackFilterInput> simpleBlackFilterInputs) throws Exception {
        if (blackKeys == null || blackKeys.size() == 0) {
            return simpleBlackFilterInputs;
        }

        List<SimpleBlackFilterInput> result = new ArrayList<>();
        for (SimpleBlackFilterInput input : simpleBlackFilterInputs) {
            boolean isInBlack = false;
            for (String key : input.simpleBlackFilterFields()) {
                if (isInBlack(key)) {
                    isInBlack = true;
                    break;
                }
            }
            if (isInBlack) {
                // do nothing
            } else {
                result.add(input);
            }
        }
        return result;
    }

    private boolean isInBlack(String key) {
        for (String blackKey : blackKeys) {
            if (key.contains(blackKey)) {
                return true;
            }
        }
        return false;
    }
}
